package util;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Helper {
    private static String alpha = new String("абвгдеёжзиыйклмнопрстуфхцчшщьэюяАБВГДЕЁЖЗИЫЙКЛМНОПРСТУФХЦЧШЩЬЭЮЯ");
    private static String[] _alpha = {"a", "b", "v", "g", "d", "e", "yo", "g", "z", "i", "y", "i",
            "k", "l", "m", "n", "o", "p", "r", "s", "t", "u",
            "f", "h", "tz", "ch", "sh", "sh", "'", "e", "yu", "ya",
            "A", "B", "V", "G", "D", "E", "YO", "G", "Z", "I", "Y", "I",
            "K", "L", "M", "N", "O", "P", "R", "S", "T", "U",
            "F", "H", "TZ", "CH", "SH", "SH", "'", "E", "YU", "YA"};
    private static String beta = new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    private static String[] _beta = {"а", "б", "ц", "д", "е", "ф", "ж", "х", "и", "ж", "к", "л",
            "м", "н", "о", "п", "к", "р", "с", "т", "у", "в",
            "в", "экс", "и", "з", "А", "Б", "Ц", "Д", "Е", "Ф", "Ж", "Х", "И", "Ж", "К", "Л",
            "М", "Н", "О", "П", "К", "Р", "С", "Т", "У", "В",
            "В", "ЭКС", "И", "З"};

    public static String translitUrl(String phrase) {
        StringBuilder newPhrase = new StringBuilder();
        for (int i = 0; i < phrase.length(); i++) {
            int k = alpha.indexOf(phrase.charAt(i));
            if (k != -1) {
                String symbol = _alpha[k];
                newPhrase.append(symbol);

            } else {
                String symbol = String.valueOf(phrase.charAt(i));
                if (symbol.equals("/") || symbol.equals(" ") || symbol.equals("\t")) {
                    symbol = "_";
                }
                newPhrase.append(symbol);
            }
        }
        return newPhrase.toString();
    }

    public static String translit(String phrase) {
        StringBuilder newPhrase = new StringBuilder();
        for (int i = 0; i < phrase.length(); i++) {
            int k = beta.indexOf(phrase.charAt(i));
            if (k != -1) {
                String symbol = _beta[k];
                newPhrase.append(symbol);

            } else {
                String symbol = String.valueOf(phrase.charAt(i));
                newPhrase.append(symbol);
            }
        }
        return newPhrase.toString();
    }

    public static String backTranslitUrl(String phrase) {
        StringBuilder newPhrase = new StringBuilder();
        for (int i = 0; i < phrase.length(); i++) {
            int k = beta.indexOf(phrase.charAt(i));
            if (k != -1) {
                String symbol = _beta[k];
                newPhrase.append(symbol);

            } else {
                String symbol = String.valueOf(phrase.charAt(i));
                if (symbol.equals("/") || symbol.equals(" ") || symbol.equals("\t")) {
                    symbol = "_";
                }
                newPhrase.append(symbol);
            }
        }
        return newPhrase.toString();
    }

    public static void main(String[] args) {
        String phrase = "Привет!";
        System.out.println(translitUrl(phrase));
    }

    public static StringBuilder readHeaderProps(String path) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(path));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("#")) continue;
            String header = line.split("=")[1];
            sb.append(";").append(header);
        }
        sb.delete(0, 1);
        reader.close();
        return sb;
    }

    public static String translitName(String name) {
        if (name.equalsIgnoreCase("brilliance")) {
            return "брилианс";
        } else if (name.equalsIgnoreCase("cadillac")) {
            return "кадилак";
        } else if (name.equalsIgnoreCase("chery")) {
            return "чери";
        } else if (name.equalsIgnoreCase("chevrolet")) {
            return "шевроле";
        } else if (name.equalsIgnoreCase("chrysler")) {
            return "крайслер";
        } else if (name.equalsIgnoreCase("citroen")) {
            return "ситроен";
        } else if (name.equalsIgnoreCase("daewoo")) {
            return "део";
        } else if (name.equalsIgnoreCase("dodge")) {
            return "додж";
        } else if (name.equalsIgnoreCase("gaz")) {
            return "газ";
        } else if (name.equalsIgnoreCase("geely")) {
            return "джили";
        } else if (name.equalsIgnoreCase("great wall")) {
            return "грейт вол";
        } else if (name.equalsIgnoreCase("hyundai")) {
            return "хендай";
        } else if (name.equalsIgnoreCase("lexus")) {
            return "лексус";
        } else if (name.equalsIgnoreCase("mercedes-benz")) {
            return "мерседес-бенц";
        } else if (name.equalsIgnoreCase("mitsubishi")) {
            return "мицубиси";
        } else if (name.equalsIgnoreCase("opel")) {
            return "опель";
        } else if (name.equalsIgnoreCase("peugeot")) {
            return "пежо";
        } else if (name.equalsIgnoreCase("porsche")) {
            return "порше";
        } else if (name.equalsIgnoreCase("renault")) {
            return "рено";
        } else if (name.equalsIgnoreCase("skoda")) {
            return "шкода";
        } else if (name.equalsIgnoreCase("ssang yong")) {
            return "санг енг";
        } else if (name.equalsIgnoreCase("toyota")) {
            return "тойота";
        } else if (name.equalsIgnoreCase("volkswagen")) {
            return "фольксваген";
        } else if (name.equalsIgnoreCase("volvo")) {
            return "вольво";
        } else if (name.equalsIgnoreCase("vortex")) {
            return "вортекс";
        } else if (name.equalsIgnoreCase("izh")) {
            return "иж";
        }
        return translit(name);
    }
}

